package com.pur.zsms.systemsetting.datadictionary.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.systemsetting.datadictionary.po.CodeType;
import com.pur.zsms.systemsetting.datadictionary.service.IDataDictionaryService;
import com.pur.zsms.systemsetting.datadictionary.vo.CodeVo;

/**
 * 
 * @author 李玉新
 * 
 */
@Controller
@RequestMapping(value = "/datadictionary")
public class DatadictionaryControllor {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(DatadictionaryControllor.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	// @Autowired
	@Resource(name = "dictionaryService")
	private IDataDictionaryService dataDictionaryService;

	@RequestMapping(value = "/datadictionarylist.do", method = { RequestMethod.POST })
	@ResponseBody
	public String codeList(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			@RequestParam(required = false) String codeType) throws Exception {

		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";

		// java.util.Map<String, Object> map = new java.util.HashMap<String,
		// Object>();
		try {
			CodeVo codevo = new CodeVo();
			PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

			this.pageList = dataDictionaryService.findCode(pageController, codevo, codeType);
			// map.put("page",pageList);

			json.put("success", true);
			json.put("total", pageList.getTotalCount());
			if (pageList.getTotalCount() > 0) {
				// json.put("success", true);
				json.put("rows", pageList.getItems());
			} else {
				// json.put("rows", "");
				// json.put("rows",pageList.getItems());
				// json.put("rows",
				// "[{\"codeId\":\"\",\"codeName\":\"\",\"codeOder\":\"\",\"codeTypeId\":\"\",\"describ\":\"\",\"isActive\":\"\",\"phCodeId\":\"\"}]");
				// json.put("rows",
				// "[{\"codeId\":null,\"codeName\":null,\"codeOder\":null,\"codeTypeId\":null,\"describ\":null,\"isActive\":\"1\",\"phCodeId\":null}]");
				// json.put("rows", "[{}]");
				json.put("rows", "[]");
			}
		} catch (Exception exception) {
			json.put("success", false);
		}
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/datadictionarylistcombo.do", method = { RequestMethod.POST })
	@ResponseBody
	public String codeListCombo(@RequestParam(required = false) String pageNo, @RequestParam(required = false) String pageSize,
			@RequestParam(required = false) String codeType) throws Exception {

		JSONObject json = new JSONObject();
		log.info("----");

		// java.util.Map<String, Object> map = new java.util.HashMap<String,
		// Object>();
		PageController pageController = new PageController();
		CodeVo codevo = new CodeVo();
		JSONArray jsonArray = new JSONArray();
		try {

			this.pageList = dataDictionaryService.findCode(pageController, codevo, codeType);
			// map.put("page",pageList);
			List<Code> list = this.pageList.getItems();

			json.put("codeId", null);
			json.put("codeName", "全部");
			jsonArray.add(0, json);
			for (int i = 0; i < list.size(); i++) {
				json.put("codeId", list.get(i).getCodeId());
				json.put("codeName", list.get(i).getCodeName());
				jsonArray.add(i + 1, json);
			}
		} catch (Exception exception) {

		}
		log.info("----***" + jsonArray.toString());
		return jsonArray.toString();

	}

	@RequestMapping(value = "/codeTypelist.do", method = { RequestMethod.POST })
	@ResponseBody
	public String codeTypeList() throws Exception {
		JSONObject json = new JSONObject();
		List<CodeType> list = dataDictionaryService.findAllCodeType();
		JSONArray jsonArray = new JSONArray();
		/*
		 * json.put("codeTypeId", null); json.put("codeTypeName", "全部");
		 */
		// jsonArray.add(0, json);
		for (int i = 0; i < list.size(); i++) {
			json.put("codeTypeId", list.get(i).getCodeTypeId());
			json.put("codeTypeName", list.get(i).getCodeTypeName());
			jsonArray.add(i, json);
		}
		log.info("----" + jsonArray.toString());
		return jsonArray.toString();

	}

	@RequestMapping(value = "/findCodeType.do", method = { RequestMethod.POST })
	@ResponseBody
	public String userList(String cId) throws GenericException {
		CodeType c = new CodeType();
		JSONObject json = new JSONObject();
		try {

			List<CodeType> list = new ArrayList();
			list = dataDictionaryService.findById(Long.parseLong(cId));
			// map.put("codetype",c);

			json.put("success", true);
			json.put("total", "1");
			json.put("rows", list);
			log.info("----" + json.toString());
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return json.toString();
	}

	/*
	 * public ModelAndView list(String cId) throws Exception {
	 * java.util.Map<String, Object> map = new java.util.HashMap<String,
	 * Object>(); CodeType c = new CodeType(); c =
	 * dataDictionaryService.findById("1"); map.put("codetype",c); return new
	 * ModelAndView("/datadictionary/dictionaryIndex").addAllObjects(map); }
	 */
	@RequestMapping(value = "/saveCodeType.do", method = { RequestMethod.POST })
	@ResponseBody
	public String saveCodeType(String rows) {
		JSONObject json = new JSONObject();
		try {
			CodeType c = new CodeType();
			JSONObject object = JSONObject.fromObject(rows);
			c = (CodeType) JSONObject.toBean(object, CodeType.class);
			System.out.println("--------" + c.getDescrib());
			dataDictionaryService.addCodetype(c);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/saveCode.do", method = { RequestMethod.POST })
	@ResponseBody
	public String saveCode(String rows) {
		JSONObject json = new JSONObject();
		try {
			Code c = new Code();
			JSONObject object = JSONObject.fromObject(rows);
			c = (Code) JSONObject.toBean(object, Code.class);
			System.out.println("--------" + c.getDescrib());
			// dataDictionaryService.addCode(c);
			dataDictionaryService.addCode(c);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/deleteCode.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteCode(String phCodeId) {
		JSONObject json = new JSONObject();
		try {

			System.out.println("--------" + phCodeId);
			dataDictionaryService.deleteCode(phCodeId);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	/**
	 * 
	 * @param codeVo
	 * @return
	 */
	@RequestMapping(value = "/getcodename.do", method = { RequestMethod.GET })
	@ResponseBody
	public String getCodeName(CodeVo codeVo) {
		JSONObject json = new JSONObject();
		try {

			/* dataDictionaryService.deleteCode(phCodeId); */
			String codeName = dataDictionaryService.findCodeName(codeVo);
			if (codeName != null && !"".equals(codeName)) {
				json.put("codeName", codeName);
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/pullDown.do", method = { RequestMethod.POST })
	public void pullDown(String codeType, String codeId, String codeName, HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		json.put("success", true);
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter writer;
		try {
			String materTypeString = dataDictionaryService.pullDownList(codeType, codeId, codeName);
			writer = response.getWriter();
			writer.write(materTypeString);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (GenericException e) {
			e.printStackTrace();
		}
		// return json.toString();
	}
}
